//
//  SISSUploadCooperationContractViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/7/9.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSUploadCooperationContractViewController.h"
#import "SISSNetworkingManager+CoopProgress.h"

@interface SISSUploadCooperationContractViewController ()

@end

@implementation SISSUploadCooperationContractViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"二手房交易合作协议";
    
    self.contractNoView.leftStr = @"*协议编号";
    self.contractNoView.rightPlaceStr = @"请输入协议编号";
    self.contractDateChooseView.leftStr = @"*协议截止日期";
    self.contractDateChooseView.rightPlaceStr = @"请选择协议截止日期";
    self.uploadImageView.titleStr = @"协议信息";
    
    if (self.cooperationContractModel) {
        self.contractNoView.rightTextField.text = self.cooperationContractModel.contractNum;
        self.contractDateChooseView.rightStr = [NSDate yyr_stringToString:self.cooperationContractModel.endTime];
        self.remarkView.rightTextField.text = self.cooperationContractModel.otherdesc;
        self.uploadImageView.imageUrlArr = self.cooperationContractModel.image.mutableCopy;
    }
}

- (void)confirmButtonClickAction {
    [super confirmButtonClickAction];
    if (self.contractNoView.rightTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入合同编号" andTime:2 andColor:YES];
        return;
    }
    if (self.contractDateChooseView.rightStr.length == 0) {
        [SISSTools showSVErrorTitle:@"请选择合同截止日期" andTime:2 andColor:YES];
        return;
    }
    if (!self.cooperationContractModel && self.uploadImageView.images.count == 0) {
        [SISSTools showSVErrorTitle:@"请添加合同文本图片" andTime:2 andColor:YES];
        return;
    }
    NSString *otherdesc = self.remarkView.rightTextField.text ? self.remarkView.rightTextField.text : @"";
   
    if (self.cooperationContractModel) {
        NSDictionary *parameters = @{
                                     @"id" : self.cooperationContractModel.ID,
                                     @"abpId" : self.abpId,
                                     @"contractNum" : self.contractNoView.rightTextField.text,
                                     @"endTime" : self.contractDateChooseView.rightStr,
                                     @"otherdesc" : otherdesc,
                                     };
        [kNetworkManager updateCooprationAgreementWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                [self pushToSuccessViewWithPopBackClassType:self.isHouseCooperatonIn ? SISSProgressPopBackClassTypeHouseCooperation : SISSProgressPopBackClassTypePassengerCooperation];
            }
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    } else {
        NSDictionary *parameters = @{
                                     @"abpId" : self.abpId,
                                     @"contractNum" : self.contractNoView.rightTextField.text,
                                     @"endTime" : self.contractDateChooseView.rightStr,
                                     @"otherdesc" : otherdesc,
                                     };
        [kNetworkManager insertCooprationAgreementWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                [self pushToSuccessViewWithPopBackClassType:self.isHouseCooperatonIn ? SISSProgressPopBackClassTypeHouseCooperation : SISSProgressPopBackClassTypePassengerCooperation];
            }
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    }
    
}

@end
